package com.wash.shoes.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.utils.uuid.IdUtils;

import java.text.MessageFormat;
import java.util.*;

/**
 * @author lijianguo
 * @Date 31/08/2024
 * code的工具类
 **/
public class CodeUtil {

    // 局部缓存
    private static ThreadLocal<Map<String, String>> codeLocal = new ThreadLocal<>();

    /**
     * @author lijianguo
     * @Date 03/09/2024
     * 清空这个线程的code
     **/
    public static void clearCurrentCacheCode() {
        codeLocal.remove();
    }

    /**
     * @author lijianguo
     * @Date 31/08/2024
     * wash code 水洗码 年月日+门店编号+随机生成5为数字就好了 2024082701xxxxx
     * 这个code有问题的，要注意不能获取连续的code
     **/
    public static String getWashCodeNext(String shopId, String shopCode) {
        //String prefix = "W" + DateUtil.format(new Date(), "yyyyMMdd") + shopCode;
        String prefix = DateUtil.format(new Date(), "yyMMdd") + shopCode.substring(0, 2);
        //String searchSql = "SELECT length(wash_code) as length, CAST(SUBSTRING(wash_code, {0}) AS UNSIGNED INTEGER) AS maxCode FROM os_shoe where shop_id={1} ORDER BY maxCode DESC LIMIT 1";
        //searchSql = MessageFormat.format(searchSql, prefix.length() + 1, shopId);
        //return getNextCode(prefix, searchSql, "washCode", 5);
        String timestamp = String.valueOf(System.nanoTime());
        // 获取当前毫秒时间戳的最后一位
        String lastDigit = timestamp.substring(timestamp.length() - 1);
        return prefix + IdUtils.randomNumberToFive() + lastDigit;
    }

    /**
     * 获取配饰的水洗码
     * @param prefix 前缀
     * @param shopCode 门店编号
     * @return
     */
    public static String getPWashCodeNext(String prefix, String shopCode ) {
        String code= getWashCodeNext(null,shopCode);
        return Objects.nonNull(prefix)?prefix+code:code;
    }

    /**
     * @author lijianguo
     * @Date 07/09/2024
     * 获取订单的code
     **/
    public static String getOrderCodeNext(String pre, String shopId, String shopCode) {
        String prefix = pre + DateUtil.format(new Date(), "yyyyMMdd") + shopCode;
        String searchSql = "SELECT length(order_no) AS length,CAST(SUBSTRING(order_no, {0}) AS UNSIGNED INTEGER ) AS maxCode " +
                "FROM os_order  WHERE shop_id = {1} ORDER BY maxCode DESC LIMIT 1";
        searchSql = MessageFormat.format(searchSql, prefix.length() + 1, shopId);
        return getNextCode(prefix, searchSql, "orderCode", 5);
    }

    /**
     * @author lijianguo
     * @Date 07/09/2024
     * 获取code
     **/
    private static String getNextCode(String prefix, String searchSql, String localKey, Integer length) {
        Map<String, String> codeMap = codeLocal.get();
        if (codeMap == null) {
            codeMap = new HashMap<>();
            codeLocal.set(codeMap);
        }
        TableCode tableCode = new TableCode();
        tableCode.setPrefix(prefix);
        tableCode.setCodeLength(prefix.length() + length);
        String rightCode = codeMap.get(localKey);
        if (rightCode == null) {
            tableCode.setSql(searchSql);
            String nextCode = TableIncCodeUtil.tableNextCode(tableCode);
            codeMap.put(localKey, nextCode);
            return nextCode;
        } else {
            Long nextNum = NumberUtil.parseLong(rightCode.substring(prefix.length(), rightCode.length()));
            // code的总长度
            String nextCode = tableCode.getNextCode(nextNum, prefix.length() + length);
            codeMap.put(localKey, nextCode);
            return nextCode;
        }
    }


}
